import server
import lib.request as req
from controllers import novel
import concurrent.futures as futures
import asyncio

# D:\Python\Project\ComfyUI_windows_portable\python_embeded\python.exe -s D:\Python\Project\ComfyUI_windows_portable\ComfyUI\main.py --cpu --windows-standalone-build

async def route(action, request):
    reqs = req.request()
    await reqs.init(request)
    # @link https://www.php.cn/faq/514746.html
    with futures.ThreadPoolExecutor(max_workers=1) as execute:
        future = execute.submit(action)
        for f in futures.as_completed([future]):
            return f.result()


if 'novel':
    @server.PromptServer.instance.routes.post("/zbq/novel/list")
    async def novel_list(request):
        # await handle(request)
        def action():
            return novel.NovelController().novel_list()

        return await route(action, request)


    @server.PromptServer.instance.routes.post("/zbq/novel/novel_save")
    async def novel_save(request):
        return await route(lambda:novel.NovelController().novel_save(), request)


    @server.PromptServer.instance.routes.post("/zbq/novel/novel_delete")
    async def novel_delete(request):
        return await route(lambda:novel.NovelController().novel_delete(), request)


if 'role':
    @server.PromptServer.instance.routes.post("/zbq/novel/role_list")
    async def role_list(request):
        return await route(lambda:novel.NovelController().novel_role_list(), request)


    @server.PromptServer.instance.routes.post("/zbq/novel/role_save")
    async def role_save(request):
        return await route(lambda:novel.NovelController().novel_role_save(), request)

    @server.PromptServer.instance.routes.post("/zbq/novel/role_delete")
    async def role_del(request):
        return await route(lambda:novel.NovelController().novel_role_del(), request)
